﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zy.WPF.CtrLib.Windows
{
    /// <summary>
    /// MessageTipWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ZyMessageTipWindow : Window, INotifyPropertyChanged
    {
        public ZyMessageTipWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        /// <summary>
        /// 消息
        /// </summary>
        private string _message;
        /// <summary>
        /// 提示消息
        /// </summary>
        public string Message
        {
            get => _message;
            set
            {
                _message = value;
                NotifyPropertyChanged("Message");
            }
        }
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler? PropertyChanged;
        /// <summary>
        /// 属性改变
        /// </summary>
        /// <param name="propName"></param>
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        #endregion
        /// <summary>
        /// 展示提示框
        /// </summary>
        /// <returns></returns>
        public new void Show()
        {
            AutoClose();
            base.Show();
        }
        /// <summary>
        /// 自动关闭弹窗的方法
        /// </summary>
        private void AutoClose()
        {
            int closeTime = 5;
            #region 自动关闭弹窗的逻辑
            //启动另一个线程
            Task.Factory.StartNew(() => {
                while (closeTime > 0)
                {
                    closeTime--;
                    Thread.Sleep(1000);
                }
                if (closeTime <= 0)
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        this.Close();
                    }));
                }
            });
            #endregion
        }
    }
}
